﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight.Command;
using PocketBook.Domain.Entities;
using System.Collections.ObjectModel;
using PocketBook.Domain.Concrete;
using PocketBook.Domain.Abstract;
using PocketBook.UI.View;
using PocketBook.UI.ViewModel.Dialogs;
using PocketBook.UI.Model;

namespace PocketBook.UI.ViewModel.Workspaces
{
    public class IncomesWorkspaceViewModel : WorkspaceViewModel
    {
        private MainWindowViewModel _parent;
        private IModelFactory _model;

        #region Constructor

        public IncomesWorkspaceViewModel(MainWindowViewModel parent)
        {
            base.DisplayName = "Incomes";
            _parent = parent;
            _model = ViewModelLocator.Kernel.Get<ModelFactory>();
            IncomesList = new ObservableCollection<Income>(_model.GetIncomes());
        }

        #endregion

        #region IncomesList

        private const string IncomesListPropertyName = "IncomesList";
        private ObservableCollection<Income> _incomesList;

        public ObservableCollection<Income> IncomesList
        {
            get
            {
                return _incomesList;
            }
            set
            {
                if (_incomesList == value)
                {
                    return;
                }
                _incomesList = value;
                RaisePropertyChanged(IncomesListPropertyName);
            }
        }

        #endregion

        #region SelectedIncome

        private const string SelectedExpensePropertyName = "SelectedIncome";
        private Income _selectedIncome;

        public Income SelectedIncome
        {
            get
            {
                return _selectedIncome;
            }
            set
            {
                if (_selectedIncome == value)
                {
                    return;
                }
                _selectedIncome = value;
                RaisePropertyChanged(SelectedExpensePropertyName);
            }
        }

        #endregion

        #region Commands

        private RelayCommand _addIncomeCommand;
        public RelayCommand AddIncomeCommand
        {
            get
            {
                if (_addIncomeCommand == null)
                {
                    _addIncomeCommand = new RelayCommand(() => this.AddIncome());
                }
                return _addIncomeCommand;
            }
        }

        private RelayCommand _editIncomeCommand;
        public RelayCommand EditIncomeCommand
        {
            get
            {
                if (_editIncomeCommand == null)
                {
                    _editIncomeCommand = new RelayCommand(() => this.EditIncome(), () => SelectedIncome != null);
                }
                return _editIncomeCommand;
            }
        }

        private RelayCommand _deleteIncomeCommand;
        public RelayCommand DeleteIncomeCommand
        {
            get
            {
                if (_deleteIncomeCommand == null)
                {
                    _deleteIncomeCommand = new RelayCommand(() => this.DeleteIncome(), () => SelectedIncome != null);
                }
                return _deleteIncomeCommand;
            }
        }

        #endregion

        #region Private Helpers

        private void AddIncome()
        {
            var win = new PocketBookDialog();
            win.DataContext = new AddOrUpdateIncomeViewModel(win, this);
            win.Title = "Pocket Book - Add expense";
            win.ShowDialog();
        }

        private void EditIncome()
        {
            var win = new PocketBookDialog();
            win.DataContext = new AddOrUpdateIncomeViewModel(win, this, _selectedIncome);
            win.Title = "Pocket Book - Edit expense";
            win.ShowDialog();
        }

        private void DeleteIncome()
        {
            var win = new PocketBookDialog();
            win.DataContext = new ConfirmDialogViewModel(
                win,
                "Are you sure you want to delete this income?",
                () => { _model.DeleteIncome(SelectedIncome.IncomeId); this.IncomesList.Remove(SelectedIncome); });
            win.Title = "Pocket Book - Confirm";
            win.ShowDialog();
        }

        #endregion
    }
}
